package popup

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import android.widget.TextView
import app.AppContext
import com.hontech.kdxclient.R
import event.DeliveryScanResultEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import protocol.BaseProtocol
import serialport.DeliverPortManager

class ReplenishScanWindow {

    companion object {
        val Instance: ReplenishScanWindow by lazy { ReplenishScanWindow() }
        var isShow = false
    }

    private val view = LayoutInflater.from(AppContext).inflate(R.layout.popup_scan_replenish, null)
    private val recyclerView = view.findViewById<RecyclerView>(R.id.id_replenish_scan_recycler_view)
    private val buttonQuit = view.findViewById<Button>(R.id.id_replenish_scan_button_quit)
    private val window = PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true)
    private val scanList = ArrayList<Data>()
    private val adapter = ItemAdapter(scanList)

    init {
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        buttonQuit.setOnClickListener(::onClickQuit)
    }

    private fun onClickQuit(view: View) {
        dismiss()
    }

    private fun init() {
        scanList.clear()
        val buf = BaseProtocol(0x05)
                .build()
        DeliverPortManager.Instance.write(buf)
        buttonQuit.text = "退出"
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onDeliveryScanResultEvent(env: DeliveryScanResultEvent) {
        if (env.state == 0x00) {
            buttonQuit.text = "扫描完成"
            return
        }
        if (env.state == 0x10) {
            buttonQuit.text = "电机堵转"
            return
        }
        if (env.state == 0x11) {
            buttonQuit.text = "电机超时"
            return
        }
        scanList.add( Data(env.row, env.col, env.state) )
        adapter.notifyItemChanged(scanList.size - 1)
        recyclerView.scrollToPosition(scanList.size - 1)
    }

    fun dismiss() {
        if (! isShow) {
            return
        }
        isShow = false
        window.dismiss()
        EventBus.getDefault().unregister(this)
    }

    fun show(view: View) {
        if (isShow) {
            return
        }
        isShow = true
        init()
        window.showAtLocation(view, Gravity.CENTER, 0, 0)
        EventBus.getDefault().register(this)
    }

}

private class Data(val row: Int, val col: Int, val state: Int) {

}

private class Item(view: View) : RecyclerView.ViewHolder(view) {

    private val textView = view.findViewById<TextView>(R.id.id_replenish_scan_item_text_view)

    fun init(data: Data) {
        val s = if (data.state == 0x01) {
            "无货"
        } else {
            "有货"
        }
        textView.text = "${data.row}-${data.col}:$s"
    }
}

private class ItemAdapter(private val list: ArrayList<Data>) : RecyclerView.Adapter<Item>() {

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(p0: Item, p1: Int) {
        p0.init(list[p1])
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): Item {
        val view = LayoutInflater.from(p0.context).inflate(R.layout.item_replenish_scan_item, p0, false)
        return Item(view)
    }
}
